package com.ms.controller;

import com.ms.common.Result;
import com.ms.pojo.Resign_apply;
import com.ms.service.UserResignService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.baomidou.mybatisplus.extension.ddl.DdlScriptErrorHandler.PrintlnLogErrorHandler.log;


@Tag(name = "员工提交离职申请接口")
@RestController
@RequestMapping("/userResign")
public class UserResignController {
    @Autowired
    private UserResignService userResignService;

    @PostMapping("/insertResignApply")
    @Operation(summary = "员工申请离职信息接口")
    public Result<String> insertResignApply(@Valid @RequestBody Resign_apply resign_apply) {
        System.out.println("后端接收的empId：" + resign_apply.getEmpId()); // 关键
        System.out.println("后端接收的resign_apply：" + resign_apply);
        try {
            int result = userResignService.insertResignApply(resign_apply);
            System.out.println("插入结果：" + result);
            if (result > 0) {
                return Result.success("离职申请提交成功");
            } else {
                return Result.error("离职申请提交失败");
            }
        } catch (IllegalArgumentException e) {
            // 捕获自定义的参数无效异常（如员工 ID 不存在）
            return Result.error(e.getMessage());
        } catch (Exception e) {
            // 捕获其他异常（如数据库错误）
            log.error("插入离职申请失败", e); // 记录详细日志
            return Result.error("系统错误，请稍后重试");
        }
    }
}
